package com.example.output;

import com.example.entity.Columns;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class WordField {
    // 字段中文名
    private String columnNameZh = "";
    
    // 字段英文名
    private String columnName;
    
    // 字段类型
    private String columnType;
    
    // 字节长度
    private String columnLen;
    
    // 填报要求
    private String columnRequire;
    
    // 字段中文注释
    private String columnDesc;
    
    public static WordField toDto(Columns columns) {
        WordField res = new WordField();
        res.setColumnName(columns.getId().getColumnName());
        res.setColumnType(columns.getDataType());
        res.setColumnLen(getColumnByteValue(columns));
        res.setColumnRequire("YES".equals(columns.getIsNullable()) ? "可选" : "必填");
        res.setColumnDesc(columns.getColumnComment());
        return res;
    }
    
    private static String getColumnByteValue(Columns columns) {
        if (columns.getCharacterOctetLength() != null) {
            return String.valueOf(columns.getCharacterOctetLength());
        }
        
        String dataType = columns.getDataType();
        switch (dataType) {
            case "tinyint":
                return "1";
            case "smallint":
                return "2";
            case "mediumint":
                return "3";
            case "int":
            case "float":
                return "4";
            case "bigint":
            case "double":
            case "datetime":
                return "8";
            case "bit":
                int bitLength = columns.getNumericPrecision().intValue();
                return String.valueOf((int) Math.ceil(bitLength * 1.0 / 8));
            default:
                return "";
        }
    }
}
